<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/27
 * Time: 14:04
 */
namespace app\appmsg\controller;

use app\common\helper\appmsg_helper;
use app\common\model\Appmsg;
use think\facade\Config;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class Admin extends AdminBase
{
    public function __call($action = '', $params = array())
    {
        $type = (int)$action;
        $category_id = Request::param('category_id', 0);
        if (!in_array($type, array(1, 2))) {
            $type = 1;
        }

        return View::fetch('index', [
            'type'        => $type,
            'category_id' => $category_id,
        ]);
    }

    public function index()
    {
        $page_no       = Request::param('page_no', 1);
        $search_filter = Request::param('search_filter', array());
        $status        = Request::param('status', 1);
        $need_store_id = Request::param('need_store_id', 1);
        $type          = Request::param('type', 0);
        $verify        = Request::param('verify', 1);
        $area_id       = Request::param('area_id', 0);

        $filter = array();
//        if (isset($search_filter['title']) && (trim($search_filter['title']) || trim($search_filter['title']) == "0")) {
//            $search_filter['title'] = trim($search_filter['title']);
//            $filter['title LIKE']   = '%'.$search_filter['title'].'%';
//        }
//        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
//            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
//            if (isset($search_filter['start_time']) && $search_filter['start_time']) {
//                $filter['add_time >='] = $search_filter['start_date'].' '.$search_filter['start_time'].':00';
//            }
//        }
//        if (isset($search_filter['stop_date']) && $search_filter['stop_date']) {
//            $filter['add_time <='] = $search_filter['stop_date'].' 23:59:59';
//            if (isset($search_filter['stop_time']) && $search_filter['stop_time']) {
//                $filter['add_time <='] = $search_filter['stop_date'].' '.$search_filter['stop_time'].':59';
//            }
//        }


        if ($type) {
            $filter['type'] = $type;
        }

        $filter['open_card_id'] = $this->open_card_id;
        $filter['verify']       = $verify;
        $filter['merge_id']     = 0;
        $filter['status']       = $status;


        $list = getList('appmsg', $filter);

        return View::fetch('index', [
            'type'          => $type,
            'search_filter' => $search_filter,
            'status'        => $status,
            'verify'        => $verify,
            'area_id'       => $area_id,
            'need_store_id' => $need_store_id,
            'list'          => $list,
        ]);
    }

    // 设置分类
    public function set_category()
    {
        return View::fetch('set_category');
    }

    public function change_status()
    {
        $status = Request::param('status', 0);
        $id = Request::param('id', 0);

        if (!$id) {
            return ['info'=>'对不起，请选择您要操作的店铺！'];
        }

        $filter = array('id' => $id);

        $appmsg_info = _uri('appmsg', $filter);

        if ($appmsg_info['status'] == $status) {
            return ['info'=>'已经执行过操作，不用重复提交'];
        }

        if ($status == 1) {
            Appmsg::update(['status' => 1], $filter);
        }

        // 操作日志
        // _widget('log')->record('appmsg_info', $id, 'update');

//        if (!$res) {
//            return ['info'=>'提交失败'];
//        }

        return array('info' => 'ok');
    }

    public function get_preview_msg()
    {
        $id = Request::param('id', 0);

        if (!$id) {
            return ['info' => '请选择要预览的图文'];
        }

        $appmsg_info = _uri('appmsg', ['id' => $id]);
        if (!$appmsg_info || $appmsg_info['status'] != 1) {
            return ['info' =>'该图文不存在'];
        }

        $appmsg_list = array();
        // 如果预览总平台的图文
//        if ($appmsg_info['store_id'] == 0) {
            $appmsg_list = appmsg_helper::get_appmsg_list($appmsg_info['id']);

//            // 如果预览店铺的图文
//        } else {
//            $appmsg_list = appmsg_helper::get_appmsg_list_for_store_preview($appmsg_info['id']);
//        }

        //an_dump($appmsg_list);

        View::assign('list', $appmsg_list);

        $template_path = MODULE_PATH."/appmsg/template/";
        $template_path .= 'msg_preview_for_store.html';

        $html = View::fetch($template_path);

        return array('info'=>'ok', 'html'=>$html);
    }

    /**
     * 取得图文列表
     */
    public function get_msg_list()
    {
        // 图文类型：1单图 2多图
        $type        = Request::post('type', 1);

        // 图文审核情况：1已审 2待审
        $verify      = Request::post('verify', 0);

        $store_id    = Request::post('store_id', 0);

        // 是否只取已同步到公众平台的图文
        $only_sync   = Request::post('only_sync', 0);

        $from_data   = Request::post('from_data', '');

        if (!$store_id) {
            $store_id = $this->open_card_id;
        }

        $filter = array(
            'type'          => $type,
            'verify'        => $verify,
            //'only_sync'     => $only_sync,
            'store_id'      => $store_id
        );

        $filter['open_card_id'] = $this->open_card_id;

        $list = appmsg_helper::get_list($filter);

        View::assign('list', $list);

        $template_path = MODULE_PATH."/appmsg/view/lib/";
        if ($type == 1) {
            $template_path .= 'select_single_list.html';
        } else {
            if ($from_data == 'send') {
                $template_path .= 'select_multi_send_list.html';
            } else {
                $template_path .= 'select_multi_list.html';
            }
        }
        $html = View::fetch($template_path);

        echo $html;
    }

}